import axios from 'axios'

const state = {
  cartCount: 0,
  testCount: 100
}

const mutations = {
  updateCartCount(state, cartCount) {
    state.cartCount += cartCount
  },
  updateCount(state, count) {
    state.testCount = count
  },
  initCartCount(state, cartCount) {
    state.cartCount = cartCount
  }
}

const getters = {
  getCount: (state) => {
    return state.cartCount
  },
  getTestCount: (state) => {
    return `this is testCount ${state.testCount}`
  }
}

const actions = {
  updateCart({commit, state}, count) {
    commit('updateCartCount', count)
  },
  getCartCount({commit, state}) {
    axios.get('/api/users/getCartCount').then((response) => {
      let res = response.data
      if (res.status === '0') {
        commit('initCartCount', res.result)
      }
    })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
